//定义页数
var pageNum;

var search = $(".search1").text();

//获取下拉框值
var snum;
var price;

$(document).ready(function(){
    var proli = $(".proList li").text();
    if(proli==null||proli==""||proli==undefined){
        ajaxpro(search, 1,snum,price);
    }
});
$(document).scroll(function () {
    var sor =  $(document).scrollTop();
    var jiaz = $(".jiaz").text();
    if(jiaz!=null&&jiaz!=""&&jiaz!=undefined) {
        var jax = $(".jiaz").position().top;
        var jx = (jax-0)-200;
        if (sor >= jx&&sor>=1653) {
            //页数加1
            pageNum++;
            $(".jiaz").remove();
            ajaxpro(search, pageNum,snum,price);
        }
    }
})
//获取排序下拉框
function xila(del) {
    $(".proList li").remove();
    $(".jiaz").remove();
    snum=null;
    price=null;
    if(del=="销量从高到低"){
        snum = 1;
    }else if(del=="销量从低到高"){
        snum = 2;
    }else if(del=="价格从高到低"){
        price = 1;
    }else if(del=="价格从低到高"){
        price = 2;
    }
    ajaxpro(search,1,snum,price);
}

function ajaxpro(search,pagenum,snum,price) {
    if(search==undefined||search==''||search==null){
        search = '';
    }
    if(snum==undefined){
        snum = null;
    }
    if(price==undefined){
        price = null;
    }
    pageNum =pagenum;
    var i = 0;
    $.ajax({
        type: "get",
        url: "http://localhost:8080/dosearch.html",
        dataType: "json",
        data: {"search":search,"pagenum":pagenum,"snum":snum,"price":price},
        success: function (data) {
            if(data!=null) {
                $.each(data, function (k, v) {
                    $(".proList").append('<li><a href="proDetail.html?id='+v.id+'"><dl><dt><img width="268" height="268" src=' + v.imagestheme + '></dt><dd class="zuijia">【最家】' + v.productname + '</dd><dd>￥' + v.commodityprices + '</dd></dl></a></li>');
                    i++;
                })
                if(i>=20) {
                    $(".proList").append("<a class='jiaz' style='margin-left: 535px;font-size: 16px'>正在加载中...</a>");
                }
                /*************鼠标悬浮*************/
                $(".proList li").on('mouseenter', function () {
                    var html = "";
                    html = '<p class="quick">快速浏览</p>';
                    $(this).css('border', '1px solid #000').append(html);
                    $(".quick").on('click', function () {
                        var index = $(".quick").prev().prev().text();
                        $.ajax({
                            url:"http://localhost:8080/quick.html",
                            data:{"id":index},
                            success:function (furnCommodity) {
                                //昵称
                                $(".proDets .fl:eq(0)").text("【最家】"+furnCommodity.productname);
                                $(".proDets .fl:eq(0)").css({"width":"630px","height":"50px","padding-bottom":"50px"});
                                //金额
                                $(".proDets .fr:eq(0)").text("￥"+furnCommodity.commodityprices);
                                //左图片
                                $(".proDets .list:eq(0)").prop("src",furnCommodity.images1);
                                $(".proDets .list:eq(0)").css({"width":"360px","height":"360px"});
                                $(".smallImg img:eq(0)").prop("src",furnCommodity.images1);
                                if(furnCommodity.images2!=''&&furnCommodity.images2!=null) {
                                    $(".smallImg img:eq(1)").prop("src", furnCommodity.images2);
                                }else{
                                    $(".smallImg img:eq(1)").remove();
                                }
                                if(furnCommodity.images3!=''&&furnCommodity.images3!=null) {
                                    $(".smallImg img:eq(2)").prop("src", furnCommodity.images3);
                                }else{
                                    $(".smallImg img:eq(2)").remove();
                                }
                                if(furnCommodity.images4!=''&&furnCommodity.images4!=null) {
                                    $(".smallImg img:eq(3)").prop("src", furnCommodity.images4);
                                }else{
                                    $(".smallImg img:eq(3)").remove();
                                }
                                $(".smallImg img").css({"width":"50px","height":"50px"});

                                //查看更多细节
                                $(".more").prop("href","proDetail.html?id="+furnCommodity.id);
                                //商品id
                                $(".commid").text(furnCommodity.id);

                            }
                        })

                        $.ajax({
                            url: "http://localhost:8080/quick2.html",
                            data: {"id": index},
                            success: function (furnVarietys) {
                                var i = 0;
                                $.each(furnVarietys,function (k,v) {
                                    //颜色分类
                                    if(v.varityname!=''&&v.varityname!=null) {
                                        $(".smallImg .fl img:eq(" + i + ")").prop("src",v.varityname);
                                        $(".smallImg .fl img:eq(" + i + ")").prop("alt",v.colorname);
                                        $(".smallImg .fl img:eq(" + i + ")").data("src",v.varityname);
                                        $(".smallImg .fl img:eq(" + i + ")").prop("class","yanse"+v.id);
                                    }else{
                                        $(".smallImg .fl:eq(" + i + ")").remove();
                                    }
                                    i++;
                                })
                                //图片替换
                                $(".smallImg img").hover(function () {
                                    var index = $(".smallImg img").index(this);
                                    var simg = $(".smallImg img:eq("+index+")").prop("src");
                                    $("[class='list']").prop("src",simg);
                                })

                                $(".mask").show();
                                $(".proDets").show();
                            }
                        })
                        $(".yanse").on("click",function () {
                            $.ajax({
                                url: "http://localhost:8080/quick3.html",
                                data:{"img":$(this).prop("src")},
                                success:function (adouble) {
                                    $(".proDets .fr:eq(0)").text("￥"+adouble);
                                }
                            })
                        })

                    });
                    $(".btns .cart").click(function () {
                        if ($(".categ p").hasClass("on")) {
                            $(".proDets").hide();
                            $(".mask").hide();
                        }
                    });
                });
                $(".proList li").on('mouseleave', function () {
                    $(this).find("p").remove();
                    $(this).css('border', '1px solid #fff');
                });
                //关闭按钮
                $(".off").click(function () {
                    $(".mask").hide();
                    $(".proDets").hide();
                    $(".pleaseC").hide();
                });
                //限制字符个数
                $(".zuijia").each(function () {
                    var maxwidth = 20;
                    if ($(this).text().length > maxwidth) {
                        $(this).text($(this).text().substring(0, maxwidth));
                        $(this).html($(this).html() + '…');
                    }
                });
            }else{
                $(".baoq").remove();
                $(".proList").append("<a class='baoq' style='margin-left: 535px;font-size: 16px'>很抱歉，没有找到与“<span style='color: red;font-weight: bold'>"+search+"</span>”相关的网页。</a>");
                $(".jiaz").remove();
            }
        }
    })
}